/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Admin
 * Date: 2022-05-07
 * Time: 18:36
 */
public class Link {

    static class NodeList {
        private NodeList next;
        private  int val;

        public NodeList(int val) {
            this.val = val;
        }
    }
    public NodeList head;

    public void clear() {
        if(head == null ){
            return;
        }
        NodeList node = this.head;
        NodeList cur = this.head.next;

        while(cur != null) {
            head.next = null;
            head = cur;
            cur = cur.next;
        }
        this.head = null;
    }
    //删除指定的所有节点
    public void removeAll(int key) {
        if(head == null) {
            return;
        }
        NodeList node = this.head;
        NodeList cur = this.head.next;

        while(cur != null) {
            if(cur.val == key) {
                node.next = cur.next;
                cur = cur.next;
            }else {
                node = cur;
                cur = cur.next;
            }
        }
        if(this.head.val == key) {
            head = head.next;
        }
    }
    public void Display() {
        NodeList node = this.head;

        while(node != null) {
            System.out.print(node.val+" ");
            node = node.next;
        }
        System.out.println();
    }

    //头插法
    public void addFirst(int data) {
        NodeList node = new NodeList(data);
        node.next = head;
        head = node;
    }
}